<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function() {
    return redirect('cats');
});

// 显示所有猫
Route::get('/cats', function() {
    $cats = App\Cat::all();
    return view('cats.index')->with('cats', $cats);
});

// 显示创建猫表单
Route::get('cats/create', function() {
    return view('cats.create');
});

// 提交创建的猫
Route::post('cats', function() {
    $cat = App\Cat::create(Request::input());
    return redirect('cats/'.$cat->id)->withSuccess('Cat has been created.');
});

// 编辑一个猫信息
Route::get('cats/{cat}/edit', function(App\Cat $cat) {
    return view('cats.edit')->with('cat', $cat);
});

// 更新编辑的猫信息
Route::put('cats/{cat}', function(App\Cat $cat) {
    $cat->update(Request::input());
    return redirect('cats/'.$cat->id)->withSuccess('Cat has been updated.');
});

// 显示某个猫的详情
Route::get('cats/{cat}', function(App\Cat $cat) {

    return view('cats.show')->with('cat', $cat);
})->where('cat', '[0-9]+');

// 显示某品种的所有猫
Route::get('cats/breeds/{name}', function( $name ) {
    $breed = App\Breed::with('cats')->whereName($name)->first();
    return view('cats.index')->with('breed', $breed)->with('cats', $breed->cats);
});

// 删除某一个猫
Route::get('cats/{cat}/delete', function(App\Cat $cat) {
    $cat->delete();
    return redirect('cats')->withSuccess('Cat has been deleted.');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
